<template>
  <div class="user-add">
    <el-card>
      <div slot="header">
        <span>添加用户</span>
      </div>
      <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userForm.username" name="username" placeholder="请输入用户名" aria-label="用户名" title="用户名"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userForm.email" name="email" placeholder="请输入邮箱" aria-label="邮箱" title="邮箱"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userForm.password" name="password" type="password" placeholder="请输入密码" aria-label="密码" title="密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="userForm.confirmPassword" name="confirmPassword" type="password" placeholder="请确认密码" aria-label="确认密码" title="确认密码"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="userForm.role" name="role" placeholder="请选择角色" aria-label="用户角色" title="用户角色">
            <el-option label="管理员" value="admin"></el-option>
            <el-option label="普通用户" value="user"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm" aria-label="添加用户" title="添加用户">添加用户</el-button>
          <el-button @click="resetForm" aria-label="重置表单" title="重置表单">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'UserAdd',
  data() {
    // 自定义确认密码验证规则
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.userForm.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }

    return {
      userForm: {
        username: '',
        email: '',
        password: '',
        confirmPassword: '',
        role: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    async submitForm() {
      try {
        const valid = await this.$refs.userForm.validate()
        if (valid) {
          // 实际项目中这里会调用API添加用户
          console.log('提交的用户数据:', this.userForm)
          this.$message.success('添加用户成功')
          this.resetForm()
        }
      } catch (error) {
        this.$message.error('请检查表单填写是否正确')
      }
    },
    resetForm() {
      this.$refs.userForm.resetFields()
    }
  }
}
</script>

<style scoped>
.user-add {
  padding: 20px;
  /* Add vendor prefixes for better cross-browser compatibility */
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  width: -webkit-fill-available;
  width: stretch;
}
</style>